import { createStore } from 'vuex'
import createPersistedstate from 'vuex-persistedstate'
//通过接口,定义对象的属性类型
interface MyState{
  loginUser:any,
  loginTime:number
}

//通过接口规范对象
const state:MyState={
  loginUser:null, 
  loginTime:0
}


export default createStore({
  plugins:[
    createPersistedstate({
      storage:window.sessionStorage
    })
  ],
  state,
  getters: {
    getUserName(state){
      return state.loginUser?state.loginUser.name:""
    },
    //getters在使用时,如果结果对应的state值没有变化,直接返回,而不是重新查询 getters只能用名称调用
    //isLogin()


  },
  mutations: {
    updateUser(state,user){
      state.loginUser =user
    },
		updateTime(state,time){
      state.loginTime =time
    }
  },
  actions: {
    login(context,user){
      context.commit("updateUser",user)
      context.commit("updateTime",new Date().getTime())

    },
    logout(context){
      context.commit("updateUser",null)
      context.commit("updateTime",0)
    },
    refreshTime(context){
      context.commit("updateTime",new Date().getTime())
    }
  },

})
